package com.xty.xtyle.base;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.xty.xtyle.R;
import com.xty.xtyle.analysis.AnalysisActivity;
import com.xty.xtyle.db.cloth.Cloth;
import com.xty.xtyle.db.cloth.ClothDao;
import com.xty.xtyle.db.mix.MixCloth;
import com.xty.xtyle.db.mix.MixDao;
import com.xty.xtyle.detail.ClothDetailActivity;
import com.xty.xtyle.mix.MixDetialActivity;
import com.xty.xtyle.utils.CommonUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


public class AnalysisTabFragment extends AbsFragment {
    private RecyclerView recyclerView;
    private View cloth_empty;
    List<Cloth> cloths;
    Map<String , List<Cloth>> types = new HashMap<>();
    ArrayList<Bean> contents = new ArrayList<>();
    String type;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_tab;
    }

    @Override
    public void initView() {
        recyclerView = findViewById(R.id.rv);
        cloth_empty = findViewById(R.id.cloth_empty);

        decode();
    }

    void link() {
        contents = new ArrayList<>();
        Set<String> keySet = types.keySet();
        for (String key : keySet) {
            Bean bean = new Bean();
            bean.content = key;
            bean.size = types.get(key).size();
            contents.add(bean);
        }
        show();
    }

    void decode() {
        Bundle bundle = getArguments();
        type = bundle.getString("type");//类型，可以，不可用
        AnalysisActivity analysisActivity = (AnalysisActivity) getActivity();
        cloths = analysisActivity.cloths;
        types = new HashMap<>();
        if (null != cloths && cloths.size() > 0) {
            switch (type) {
                case "类型":
                    checkType(); //获取类型中所有数据,并进行分类
                    link();
                    break;
                case "颜色":
                    checkColor();//获取颜色中所有数据,并进行分类统计数量
                    link();
                    break;
                case "面料":
                    checkMaterial();//获取面料中所有数据,并进行分类统计数量
                    link();
                    break;
                case "季节":
                    checkSession();//获取季节中所有数据,并进行分类统计数量
                    link();
                    break;
                case "品牌"://获取品牌中所有数据,并进行分类统计数量
                    checkBrand();
                    link();
                    break;
                case "状态":
                    checkStatus();//获取状态中所有数据,并进行分类统计数量
                    link();
                    break;
                case "穿戴":
                    checkMix();//获取搭配中所有数据,并进行分类统计数量
                    break;
                case "价格":
                    checkPrice();//获取价格中所有数据,并进行分类统计数量
                    break;
            }
        }
    }
    void checkPrice() {
        long total = 0;
        contents = new ArrayList<>();
        for (Cloth cloth:cloths) {
            String type = cloth.getPrice();
            try {
                int pri = Integer.valueOf(type);
                total += pri;
            }catch (Exception e) {

            }
            Bean bean = new Bean();
            bean.content = "价格为：￥ "+type;
            bean.id = cloth.getId();
            bean.image = cloth.getImage();
            bean.size = 1;
            contents.add(bean);
        }
        TextView textView = findViewById(R.id.total);
        textView.setVisibility(View.VISIBLE);
        textView.setText("衣服总价为：    ￥ "+total);
        show();
    }

    void checkType() {
        for (Cloth cloth:cloths) {
            String type = cloth.getType();
            if (TextUtils.isEmpty(type)) continue;
            if (exist(cloth.getType())) {
                List<Cloth> subCloths = types.get(type);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(cloth.getType(), subCloths);
            }
        }
    }

    void checkColor() {
        for (Cloth cloth:cloths) {
            String color = cloth.getColor();
            if (TextUtils.isEmpty(color)) continue;
            if (exist(color)) {
                List<Cloth> subCloths = types.get(color);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(color, subCloths);
            }
        }
    }

    void checkMaterial() {
        for (Cloth cloth:cloths) {
            String material = cloth.getMaterial();
            if (TextUtils.isEmpty(material)) continue;
            if (exist(material)) {
                List<Cloth> subCloths = types.get(material);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(material, subCloths);
            }
        }
    }

    void checkSession() {
        for (Cloth cloth:cloths) {
            String ss = cloth.getSession();
            if (TextUtils.isEmpty(ss)) continue;
            if (exist(ss)) {
                List<Cloth> subCloths = types.get(ss);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(ss, subCloths);
            }
        }
    }

    void checkBrand() {
        for (Cloth cloth:cloths) {
            String brand = cloth.getBrand();
            if (TextUtils.isEmpty(brand)) continue;
            if (exist(brand)) {
                List<Cloth> subCloths = types.get(brand);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(brand, subCloths);
            }
        }
    }

    void checkStatus() {
        for (Cloth cloth:cloths) {
            String status = cloth.getStatus();
            if (TextUtils.isEmpty(status)) continue;
            if (exist(status)) {
                List<Cloth> subCloths = types.get(status);
                subCloths.add(cloth);
            }else {
                List<Cloth> subCloths = new ArrayList<>();
                subCloths.add(cloth);
                types.put(status, subCloths);
            }
        }
    }

    void checkMix() {
        MixDao mixDao = new MixDao();
        List<MixCloth> mixCloths = mixDao.queryAll();
        ArrayList<Integer> ids = new ArrayList<>();//所有的id
        //Long为id, integer为次数
        Map<Integer, Integer> map = new HashMap<>();
        for (MixCloth mixCloth : mixCloths) {

            ids.add(mixCloth.getUpId());
            ids.add(mixCloth.getDownId());
            ids.add(mixCloth.getPieUpId());
            ids.add(mixCloth.getPieCenterId());
            ids.add(mixCloth.getPieDownId());
        }
        for (int i : ids) {
            if (i< 1) continue;
            if (existId(i, map)) {
                int val = map.get(i);
                map.put(i, ++val);
            }else {
                map.put(i, 1);
            }
        }
        contents = new ArrayList<>();
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            int mapKey = entry.getKey();
            int times = entry.getValue();
            Bean bean = new Bean();
            bean.id = mapKey;
            bean.size = times;
            bean.content = "穿戴次数为："+times;
            contents.add(bean);
        }
        show();

    }
    boolean existId(int id, Map<Integer, Integer> map) {
        Set<Integer> keySet = map.keySet();
        if (keySet.contains(id)) return true;
        return false;
    }

    Cloth loadCloth(int id) {
        if (id <= 0) return null;
        ClothDao clothDao = new ClothDao();
        Cloth cloth = clothDao.findCloth(id);
        return cloth;
    }

    void show() {
        if (null ==  contents|| contents.isEmpty()) {
            showEmpty(true);
        }else {
            showEmpty(false);
            recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),1));
            recyclerView.setAdapter(new StringAdapter());
        }
    }


    boolean exist(String key) {
        Set<String> keySet = types.keySet();
        if (keySet.contains(key)) return true;
        return false;
    }


    @Override
    public void freshData() {

    }

    private void showEmpty(boolean show) {
        if (show) {
            cloth_empty.setVisibility(View.VISIBLE);
        }else {
            cloth_empty.setVisibility(View.GONE);
        }
    }

    private class StringAdapter extends RecyclerView.Adapter<StringAdapter.VH> {

        @NonNull
        @Override
        public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            return new VH(LayoutInflater.from(parent.getContext()).inflate( R.layout.item_analysis, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull VH holder, int position) {


            if ("穿戴".equals(type)) {
                final Bean bean = contents.get(position);
                ClothDao dao = new ClothDao();
                Cloth cl = dao.findCloth(bean.id);
                if (null != cl && !TextUtils.isEmpty(cl.getImage())) {
                    Glide.with(holder.analysisImage).load(cl.getImage()).into(holder.analysisImage);
                }
                holder.textContent.setText(bean.content);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ClothDetailActivity.startActivity(view.getContext(), bean.id);
                    }
                });
            }else {
                final Bean bean = contents.get(position);
                if (!TextUtils.isEmpty(bean.image))
                    Glide.with(holder.analysisImage).load(bean.image).into(holder.analysisImage);
                holder.textView.setText(bean.content);
                holder.textContent.setText("数量为："+bean.size);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if ("价格".equals(type)) {
                            ClothDetailActivity.startActivity(view.getContext(), bean.id);
                        }else
                            ListActivity.startActivity((Activity) view.getContext(), type, bean.content);
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return contents.size();
        }

        class VH extends RecyclerView.ViewHolder {
            TextView textView;
            TextView textContent;
            ImageView analysisImage;
            public VH(@NonNull View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.tv_title);
                textContent = itemView.findViewById(R.id.tv_content);
                analysisImage = itemView.findViewById(R.id.analysisIcon);
            }
        }
    }

    class Bean {
        public String content;
        public int size;
        public int id;
        public String image;
    }
}
